package org.jeecg.modules.demo.service.impl;

import org.jeecg.modules.demo.mapper.CeshiMain001Mapper;
import org.jeecg.modules.demo.mapper.CeshiSub_01Mapper;
import org.jeecg.modules.demo.entity.CeshiMain001;
import org.jeecg.modules.demo.entity.CeshiSub_01;
import org.jeecg.modules.demo.service.ICeshiMain001Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 钉钉单号
 * @Author: jeecg-boot
 * @Date:   2022-11-25
 * @Version: V1.0
 */
@Service
public class CeshiMain001ServiceImpl extends ServiceImpl<CeshiMain001Mapper, CeshiMain001> implements ICeshiMain001Service {

	@Autowired
	private CeshiMain001Mapper ceshiMain001Mapper;
	@Autowired
	private CeshiSub_01Mapper ceshiSub_01Mapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CeshiMain001 ceshiMain001, List<CeshiSub_01> ceshiSub_01List) {
		ceshiMain001Mapper.insert(ceshiMain001);
		if(ceshiSub_01List!=null && ceshiSub_01List.size()>0) {
			for(CeshiSub_01 entity:ceshiSub_01List) {
				//外键设置
				entity.setOrderId(ceshiMain001.getId());
				ceshiSub_01Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CeshiMain001 ceshiMain001,List<CeshiSub_01> ceshiSub_01List) {
		ceshiMain001Mapper.updateById(ceshiMain001);
		
		//1.先删除子表数据
		ceshiSub_01Mapper.deleteByMainId(ceshiMain001.getId());
		
		//2.子表数据重新插入
		if(ceshiSub_01List!=null && ceshiSub_01List.size()>0) {
			for(CeshiSub_01 entity:ceshiSub_01List) {
				//外键设置
				entity.setOrderId(ceshiMain001.getId());
				ceshiSub_01Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		ceshiSub_01Mapper.deleteByMainId(id);
		ceshiMain001Mapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ceshiSub_01Mapper.deleteByMainId(id.toString());
			ceshiMain001Mapper.deleteById(id);
		}
	}
	
}
